package com.jianyun.jyzs.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jianyun.jyzs.R;
import com.jianyun.jyzs.adapter.PDAdapter;
import com.jianyun.jyzs.bean.EngineeringResult;
import com.jianyun.jyzs.bean.PDDetial;
import com.jianyun.jyzs.bean.TimeEngineer;
import com.jianyun.jyzs.dao.EngLocationDao;
import com.jianyun.jyzs.dao.EngineerDao;
import com.jianyun.jyzs.http.PDHttpHelper;
import com.jianyun.jyzs.utils.DateTime;
import com.jianyun.jyzs.utils.LoginCache;
import com.jianyun.jyzs.utils.UtilsStyle;
import com.jrmf360.rylib.common.util.ToastUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by mlli on 2017/11/11.
 */

public class PersonalDyamicActivity extends AppCompatActivity implements View.OnClickListener {

    @BindView(R.id.topBack)
    ImageView topBack;
    @BindView(R.id.topText)
    TextView topText;
    @BindView(R.id.tv_checked)
    TextView tvChecked;
    @BindView(R.id.tv_update)
    TextView tvUpdate;
    @BindView(R.id.iv_more)
    ImageView ivMore;
    @BindView(R.id.iv_moreLinearLayout)
    LinearLayout ivMoreLinearLayout;
    @BindView(R.id.topCreat)
    LinearLayout topCreat;
    @BindView(R.id.iv_single)
    ImageView ivSingle;
    @BindView(R.id.iv_group)
    ImageView ivGroup;
    @BindView(R.id.topChatDetail)
    LinearLayout topChatDetail;
    @BindView(R.id.topId)
    RelativeLayout topId;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private EngineerDao dao;
    private PDAdapter adapter;
    private List<TimeEngineer> mDatas;
    private List<String> timeList = new ArrayList<>();
    private boolean tag = true;
    private String userId;
    private String enterpriseCode;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //UtilsStyle.setStatusBarMode(this,true);
        setContentView(R.layout.ac_personal_dyamic);
        ButterKnife.bind(this);
        init();
    }

    protected void init() {
        topBack.setVisibility(View.VISIBLE);
        topBack.setOnClickListener(this);
        topText.setVisibility(View.VISIBLE);
        topText.setText("我的动态");
        dao = new EngineerDao(this);
        userId = LoginCache.getInstance().getUserInfo().getUserId();
        enterpriseCode = LoginCache.getInstance().getLoginCache().getEnterpriseCode();
        LoginCache instance = LoginCache.getInstance();
        mDatas = new ArrayList<>();
        List<EngineeringResult.EngineerBean> engineer = dao.getEngineerByUser2(userId, enterpriseCode);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new PDAdapter(this, null,userId);
        adapter.setUserName(instance.getUserDetaileInfo().getUserName());
        //自己的头像
        String portraitUrl = instance.getLoginCache().getErpRootUrl()+instance.getUserDetaileInfo().getIconPaths();
        if (portraitUrl!=null){
            adapter.setHeadImgPath(portraitUrl);
        }
        recyclerView.setAdapter(adapter);
        transformTimerShaft(engineer);
        initData();
    }



    //转换数据适配个人动态适配器
    private void transformTimerShaft(List<EngineeringResult.EngineerBean> engineer) {
        if (engineer != null) mDatas.clear();
        //先得到日期标签 共三次遍历恶心  如果有更好的方法替换
        for (EngineeringResult.EngineerBean ee : engineer) {
            String day = DateTime.YMD(ee.getDtLong());
            if (timeList.contains(day)) {
                continue;
            } else {
                timeList.add(day);
            }
        }
        for (String s : timeList) {
            ArrayList<EngineeringResult.EngineerBean> list = new ArrayList<>();
            for (EngineeringResult.EngineerBean ee : engineer) {
                if (DateTime.YMD(ee.getDtLong()).equals(s)) list.add(ee);
            }
            mDatas.add(new TimeEngineer(list, s, 1));
        }


        int count = new EngLocationDao(this).getEngineerCount();
        TimeEngineer timeEngineer = new TimeEngineer(null, null, 0);
        PDDetial detial = new PDDetial();
        detial.setPart(count);
        detial.setUploadCount(dao.getEngineerCount(userId));
        detial.setUserId(userId);
        timeEngineer.setPdDetial(detial);
        mDatas.add(0, timeEngineer);
        adapter.setList(mDatas, 2);


    }

    private ArrayList<TimeEngineer> hehe(List<TimeEngineer> mDatas) {
        ArrayList<TimeEngineer> list = new ArrayList<>();
        for (int i = mDatas.size() - 1; i >= 0; i--) {
            list.add(mDatas.get(i));
        }
        return list;
    }


    private void initData() {
        HashMap<String, String> map = new HashMap<>();
        map.put("method","GePersonalProjectProgress");
        map.put("userId", userId);
        map.put("enterpriseCode", enterpriseCode);
        map.put("appCode", "jyzs");
        map.put("appType", "Android");
        map.put("appVersion", "1.0.2");
        PDHttpHelper.getPersonalDynamic(dao, map, new PDHttpHelper.HttpResultListener() {
            @Override
            public void resultListener(boolean b, String msg) {
                if (b) {
                    List<EngineeringResult.EngineerBean> engineer = dao.getEngineerByUser2(userId, enterpriseCode);
                    transformTimerShaft(engineer);
                } else {
                    ToastUtil.showNoWaitToast(PersonalDyamicActivity.this, msg);
                }
            }
        });
    }




    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.topBack:
                finish();
                break;
        }
    }
}
